package com.litchi.tutor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.litchi.tutor.common.entity.Dict;
import com.litchi.tutor.common.entity.Recruit;
import com.litchi.tutor.common.entity.Region;
import com.litchi.tutor.common.entity.User;
import com.litchi.tutor.common.util.CopyUtil;
import com.litchi.tutor.common.util.JwtUtil;
import com.litchi.tutor.common.vo.RecruitResp;
import com.litchi.tutor.mapper.DictMapper;
import com.litchi.tutor.mapper.RecruitMapper;
import com.litchi.tutor.mapper.RegionMapper;
import com.litchi.tutor.mapper.UserMapper;
import com.litchi.tutor.service.RecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyouzi
 * @since 2021-11-10
 */
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper, Recruit> implements RecruitService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    RegionMapper regionMapper;

    @Override
    public void createRecruit(Recruit recruit, String title, Map<String, String> map, HttpServletRequest request) {
        Recruit newRecruit = new Recruit();
        BeanUtils.copyProperties(recruit, newRecruit);
        String token = request.getHeader("Authorization");
        Integer id = JwtUtil.getId(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
        newRecruit.setUserid(user.getUserid());
        save(newRecruit);

        // TODO 消息推送

    }

    @Override
    public List<RecruitResp> getRecruit(Long current, Long limit, String area, String teachingSubjectIds, String other, String gradeId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Integer id = JwtUtil.getId(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", id));

        Page<Recruit> pageRecruit = new Page<>(current,limit);
        QueryWrapper<Recruit> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(area)) {
            queryWrapper.eq("area", area);
        }

        if (!StringUtils.isEmpty(teachingSubjectIds)) {
            String[] split = teachingSubjectIds.split(",");
            for (String s : split) {
                queryWrapper.or().like("teaching_subject_ids", s);
            }
        }

        if (!StringUtils.isEmpty(other) && other.equals("other")) {
            queryWrapper.ne("custom_teaching_subject", "");
        }

        if (!StringUtils.isEmpty(gradeId)) {
            String[] split = gradeId.split(",");
            for (String s : split) {
                queryWrapper.or().like("grade_id", s);
            }
        }

        queryWrapper.eq("userid", user.getUserid());

        page(pageRecruit, queryWrapper);
        List<Recruit> records = pageRecruit.getRecords();
        records.forEach(item->{
            String phoneNumber = item.getMobile().substring(0, 3) + "****" + item.getMobile().substring(7, item.getMobile().length());
            item.setMobile(phoneNumber);
        });

        List<RecruitResp> newList = CopyUtil.copyList1(records, RecruitResp.class);
        newList.forEach(item -> {
            Dict grade_id = dictMapper.selectOne(new QueryWrapper<Dict>().eq("id", item.getGradeId()));
            item.setGradeText(grade_id.getText());

            StringBuffer teachingSubjectText = changeIdsToText(item.getTeachingSubjectIds());
            item.setTeachingSubjectText(teachingSubjectText.toString());

            StringBuffer teachingTimesText = changeIdsToText(item.getTeachingTimesIds());
            item.setTeachingTimesText(teachingTimesText.toString());

            StringBuffer teachingDurationText = changeIdsToText(item.getTeachingDurationIds());
            item.setTeachingDurationText(teachingDurationText.toString());

            Region region = regionMapper.selectOne(new QueryWrapper<Region>().eq("code", item.getArea()));
            item.setAreaText(region.getName());
        });
        return newList;
    }

    public StringBuffer changeIdsToText(String ids) {
        List<Dict> teachingGradeIds = dictMapper.selectBatchIds(Arrays.asList(ids.split(",")));
        StringBuffer stringBuffer = new StringBuffer();
        teachingGradeIds.forEach(item -> {
            stringBuffer.append(item.getText()).append(",");
        });
        return stringBuffer;
    }
}
